<script setup lang="ts">
import { ref } from "vue";
import { useECharts } from "@pureadmin/utils";
import NullPng from "../../menu-content/null-png.vue";

const props = defineProps({
  data: {
    type: Object,
    default: () => []
  },
  dataList: {
    type: Object,
    default: () => []
  },
  legendList: {
    type: Object,
    default: () => []
  },
  color: {
    type: Array,
    default: () => []
  },
});

const getxAxis = () => {
  let res = []
  if (props.dataList?.xAxis?.length > 0) {
    props.dataList?.xAxis.forEach(item => {
      res.push(item)
    })
    return res
  }

}
const getData = () => {
  let res = []
  if (props.dataList?.data?.length > 0) {
    props.dataList?.data.forEach((item, index) => {
      res.push(
        {
          data: item.value,
          name: item.name,
          type: 'line',
          smooth: true,
          markLine: {
            silent: false,
          },
          areaStyle: {
            opacity: 0.6,
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {
                offset: 0,
                color: props.color[index % props.color.length]
              },
              {
                offset: 1,
                color: 'rgba(0, 0, 0, 0)'
              },
            ])
          },
        })
      // console.log("getData",item);
    })
    return res
  }

}


const chartRef = ref();
const { setOptions } = useECharts(chartRef, {
  renderer: "svg"
});
watch(() => props, () => {
  // console.log("曲线图数据来了",props);
  nextTick(() => {
    setOptions({
      //  ['#FDBC52', '#4583FF', '#07C7E0', ]
      color: props.color,
      container: "#screen",
      legend: {
        data: props.legendList,
        left: '0',
        top: '4',
        textStyle: {
          color: '#fff',
          fontSize: 14
        }

      },
      dataZoom: [
        //滑动条
        {
          xAxisIndex: 0, //这里是从X轴的0刻度开始
          show: true, //是否显示滑动条，不影响使用
          type: "inside", // 这个 dataZoom 组件是 slider 型 dataZoom 组件
          startValue: 100, // 从头开始。
          endValue: 12, // 一次性展示几个。
        },
      ],
      tooltip: {
        trigger: 'axis',
      },
      xAxis: {
        type: 'category',
        data: getxAxis(),
        nameLocation: 'start'
      },


      yAxis: {
        show: false,
        type: 'value',
        splitLine: {   //去除网格线
          show: false,
        }
      },
      grid: props.data,
      series: getData()
    });
  })
}, {
  immediate: true,
  deep: true
})


</script>
<template>
  <!--  :style="{ height: props.data[1].height2 }" -->
  <div ref="chartRef" class="w-full"  v-if="props.dataList?.data?.length > 0"/>
  <NullPng v-else></NullPng>
</template>
